﻿local L = TWEETCRAFT_LOCAL_STRINGS

assert(LibStub:GetLibrary("LibTweetCraft-1.2", true), "TweetCraft requires LibTweetCraft-1.2")
assert(LibStub:GetLibrary("LibDataBroker-1.1", true), "TweetCraft requires LibDataBroker-1.1")

-- Create a local variable that holds the LibTweetCraft library
local LibTweetCraft = LibStub:GetLibrary("LibTweetCraft-1.2");
local LibDataBroker = LibStub:GetLibrary("LibDataBroker-1.1")

-- Create data object for TweetCraft launcher
LibDataBroker:NewDataObject("TweetCraft", {
		type = "launcher",
		icon = "Interface\\Addons\\TweetCraft\\Textures\\UI-TweetCraft-TweetCraftIcon",
		OnClick = function(frame, button)
			TweetCraft:ToggleTweetCraft()
		end,
		OnEnter = function(self)
	        TweetCraft.AddNewbieTip(self, L.TWEETCRAFT, 1, 1, 1, L.TWEETCRAFTTOOLTIP, 1)
		end,
		OnLeave = function()
			GameTooltip:Hide()			
		end
	})

-- Construct TweetCraft table
TweetCraft = {}

function TweetCraft.AddNewbieTip(pItem, pNormalText, pRed, pGreen, pBlue, pNewbieText, pNoNormalText)
	if SHOW_NEWBIE_TIPS == "1" then
		GameTooltip_SetDefaultAnchor(GameTooltip, pItem)
		if pNormalText then
			GameTooltip:SetText(pNormalText, pRed, pGreen, pBlue)
			GameTooltip:AddLine(pNewbieText, NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b, 1)
		else
			GameTooltip:SetText(pNewbieText, pRed, pGreen, pBlue, 1, 1)
		end
		GameTooltip:Show()
	else
		if not pNoNormalText then
			GameTooltip:SetOwner(pItem, "ANCHOR_RIGHT")
			GameTooltip:SetText(pNormalText, pRed, pGreen, pBlue)
		end
	end
end

function TweetCraft.ToggleTweetCraft(command)
	if command ~= nil then
		if strlower(command) == "help" then
			TweetCraftHelpFrame:Show()
		else
			print("Unknown TweetCraft command.")
		end
	else
		-- Determine whether to hide or show the TweetCraftFrame
		if TweetCraftFrame:IsShown() then
			HideUIPanel(TweetCraftFrame)
		else
			ShowUIPanel(TweetCraftFrame)
		end
	end
	
end

function TweetCraft.Tweet(message)
	if not message  or strlen(message) == 0 then
		print(L.SLASHTWEETUSAGE)
		return
	end
	-- Print "Tweeting '<message>'..." to the chat window
	print(L.TWEETINGFORMAT:format(message))
	
	-- Tweet the message and send it
	LibTweetCraft:Tweet(message)
	LibTweetCraft:Send(true)
end

function TweetCraft:Initialize()
	-- Register simple slash command for LibTweetCraft:Tweet
	SLASH_TWEET1 = L.SLASHTWEET1
	SlashCmdList["TWEET"] = self.Tweet

	-- Register slash command for showing or hiding TweetCraftFrame
	SLASH_TWEETCRAFT1 = L.SLASHTWEETCRAFT1
	SlashCmdList["TWEETCRAFT"] = self.ToggleTweetCraft
	
	-- Initialize minimap button options
	if not tweetCraftOptions.ShowMinimapButton then
		tweetCraftOptions.ShowMinimapButton = true
	end
	
	if not tweetCraftOptions.MinimapButtonAngle
	and not tweetCraftOptions.MinimapButtonX then
		tweetCraftOptions.MinimapButtonAngle = -1.5708
	end
end

function TweetCraft:AddOnLoaded()
	-- Initialize minimap button
	if tweetCraftOptions.ShowMinimapButton then
		TweetCraftMinimapButton:Show()
	else
		TweetCraftMinimapButton:Hide()
	end
	
	if tweetCraftOptions.MinimapButtonAngle then
		self.MinimapButton_SetPositionAngle(tweetCraftOptions.MinimapButtonAngle)
	else
		self.MinimapButton_SetPosition(tweetCraftOptions.MinimapButtonX, tweetCraftOptions.MinimapButtonY)
	end
	
	if not dontShowTweetCraftHelpAgain then
		TweetCraftHelpFrame:Show()
	end
end

function TweetCraft:HasOutgoingTweets()
	return #(LibTweetCraft:GetOutgoingTweets()) > 0
end

-- start http://www.wowwiki.com/Wait

local waitTable = {};
local waitFrame = nil;

function TweetCraft:Wait(delay, func, ...)
  if(type(delay)~="number" or type(func)~="function") then
    return false;
  end
  if(waitFrame == nil) then
    waitFrame = CreateFrame("Frame","WaitFrame", UIParent);
    waitFrame:SetScript("onUpdate",function (self,elapse)
      local count = #waitTable;
      local i = 1;
      while(i<=count) do
        local waitRecord = tremove(waitTable,i);
        local d = tremove(waitRecord,1);
        local f = tremove(waitRecord,1);
        local p = tremove(waitRecord,1);
        if(d>elapse) then
          tinsert(waitTable,i,{d-elapse,f,p});
          i = i + 1;
        else
          count = count - 1;
          f(unpack(p));
        end
      end
    end);
  end
  tinsert(waitTable,{delay,func,{...}});
  return true;
end
-- end http://www.wowwiki.com/Wait
